#include "md5wrapper.h"

#ifdef __cplusplus
extern "C" {
#endif
#include "encyptwrapper/libmd-0.3/md5.h"
#ifdef __cplusplus
}
#endif

int mdfile(FILE *fp, unsigned char *digest)
{
    unsigned char buf[4096];
    MD5_CTX ctx;
    int n;

    MD5Init(&ctx);
    while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
        MD5Update(&ctx, buf, n);
    MD5Final(digest, &ctx);
    if (ferror(fp))
        return -1;
    return 0;
}


int mdfile(const char *path, unsigned char *digest)
{
    int ret = 0;
    FILE *fp = fopen(path, "rb");
    if(fp){
        ret = mdfile(fp, digest);
        fclose(fp);
    }else{
        perror(path);
        ret = -1;
    }
    return ret;
}
